﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using model;
using System.Data;
using System.Data.SqlClient;

namespace dao
{
    public class NhomSanphamTrathatDao:BaseDao
    {
        public List<NhomSanphamTrathat> getListAllNhomsanpham()
        {
            sqlCmd.CommandType = CommandType.StoredProcedure;
            sqlCmd.CommandText = "sp_Nhomsanpham_SelectAll";
            sqlCmd.Parameters.Clear();
            sqlCmd.Connection.Close();
            sqlCmd.Connection.Open();

            using (SqlDataReader dr = sqlCmd.ExecuteReader())
            {
                List<NhomSanphamTrathat> glstNhomsanpham = new List<NhomSanphamTrathat>();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        NhomSanphamTrathat sp = new NhomSanphamTrathat();
                        sp.ID = Convert.ToInt64(dr["ID"]);
                        sp.Tennhom = Convert.ToString(dr["Tennhom"]);
                        glstNhomsanpham.Add(sp);
                    }
                    dr.Close();
                    sqlCmd.Connection.Close();
                }
                return glstNhomsanpham;
            }
        }

        public List<NhomSanphamTrathat> getListNhomSanphamByID(long id)
        {
            sqlCmd.CommandType = CommandType.StoredProcedure;
            sqlCmd.CommandText = "sp_Nhomsanpham_SelectRow";
            sqlCmd.Parameters.Clear();
            sqlCmd.Parameters.Add("@ID", SqlDbType.BigInt).Value = id;
            sqlCmd.Connection.Close();
            sqlCmd.Connection.Open();

            using (SqlDataReader dr = sqlCmd.ExecuteReader())
            {
                List<NhomSanphamTrathat> glstNhomsanpham = new List<NhomSanphamTrathat>();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        NhomSanphamTrathat sp = new NhomSanphamTrathat();
                        sp.ID = Convert.ToInt64(dr["ID"]);
                        sp.Tennhom = Convert.ToString(dr["Tennhom"]);
                        glstNhomsanpham.Add(sp);
                    }
                    dr.Close();
                    sqlCmd.Connection.Close();
                }
                return glstNhomsanpham;
            }
        }

        public string themmoiNhomSanpham(NhomSanphamTrathat bv)
        {
            sqlCmd.CommandType = CommandType.StoredProcedure;
            sqlCmd.CommandText = "sp_Nhomsanpham_Insert";
            sqlCmd.Parameters.Clear();

            sqlCmd.Parameters.Add("@Tennhom", SqlDbType.NVarChar).Value = bv.Tennhom;
            sqlCmd.Connection.Open();
            sqlCmd.ExecuteNonQuery();
            sqlCmd.Connection.Close();

            return sqlCmd.Parameters["@Tennhom"].Value.ToString();
        }

        public string suaNhomSanpham(NhomSanphamTrathat bv)
        {
            sqlCmd.CommandType = CommandType.StoredProcedure;
            sqlCmd.CommandText = "sp_Nhomsanpham_Update";
            sqlCmd.Parameters.Clear();

            sqlCmd.Parameters.Add("@ID", SqlDbType.BigInt).Value = bv.ID;
            sqlCmd.Parameters.Add("@Tennhom", SqlDbType.NVarChar).Value = bv.Tennhom;
            sqlCmd.Connection.Open();
            sqlCmd.ExecuteNonQuery();
            sqlCmd.Connection.Close();

            return sqlCmd.Parameters["@ID"].Value.ToString();
        }
    }
}
